layui.define(function (exports) {
    var EventBus = function () {}

    EventBus.prototype.tableMap = {};
    EventBus.prototype.table = function (id, event, callback) {
        if (this.tableMap[id] instanceof TableManage) {
            this.tableMap[id].on(event, callback);
        } else {
            this.tableMap[id] = new TableManage(id);
            this.tableMap[id].on(event, callback);
        }
    }

    /** 数据表格-工具栏事件 */
    var TableManage = function (id) {
        var eventMap = this.eventMap = {};

        // 绑定数据表格的工具栏点击事件
        layui.onevent('table', `tool(${id})`, function (obj) {
            var event = eventMap[obj.event];

            if (typeof event === 'function') {
                event.call(this, obj);
            } else {
                console.error('事件未绑定', obj);
            }
        });
    }

    TableManage.prototype.on = function (event, fn) {
        this.eventMap[event] = fn;
    }

    // 暴露事件总线
    exports('eventbus', new EventBus());
});